#include "gwbase/base/status.h" 
#include <stdio.h> 
#include <string.h>

namespace dicom {
namespace {

const char* StatusCodeToString(Status::Code code) {
  switch (code) {
    case Status::kOk:
      return "OK";
    case Status::kIOError:
      return "IO";
    case Status::kNetworkError:
      return "NET";
    case Status::kOther:
    default:
      return "OTH";
  }
}

Status PosixError(Status::Code code, const std::string& context, 
                  int error_num) 
{
  return Status(code, error_num, context, strerror(error_num));
}

}

std::string Status::ToString() const {
  char buf[64];
  snprintf(buf, sizeof(buf), "[%s:%d]: ", 
           StatusCodeToString(code_), subcode_);

  return std::string(buf) + msg_;
}

Status IOError(const std::string& context, int err_no) {
  return PosixError(Status::kIOError, context, err_no);
}

Status NetworkError(const std::string& context, int socket_err_no) {
  return PosixError(Status::kNetworkError, context, socket_err_no);
}

}
